package cn.powerone.scm.mapper;

import java.util.Date;
import java.util.List;
import cn.powerone.scm.domain.ScmCustomerOrders;
import cn.powerone.scm.domain.ScmCustomerOrderItems;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

/**
 * 客户订单Mapper接口
 * 
 * @author PowerOne
 * @date 2025-07-04
 */
public interface ScmCustomerOrdersMapper 
{
    /**
     * 查询客户订单
     * 
     * @param id 客户订单主键
     * @return 客户订单
     */
    public ScmCustomerOrders selectScmCustomerOrdersById(Long id);

    /**
     * 查询客户订单列表
     * 
     * @param scmCustomerOrders 客户订单
     * @return 客户订单集合
     */
    public List<ScmCustomerOrders> selectScmCustomerOrdersList(ScmCustomerOrders scmCustomerOrders);

    /**
     * 新增客户订单
     * 
     * @param scmCustomerOrders 客户订单
     * @return 结果
     */
    public int insertScmCustomerOrders(ScmCustomerOrders scmCustomerOrders);

    /**
     * 修改客户订单
     * 
     * @param scmCustomerOrders 客户订单
     * @return 结果
     */
    public int updateScmCustomerOrders(ScmCustomerOrders scmCustomerOrders);

    /**
     * 删除客户订单
     * 
     * @param id 客户订单主键
     * @return 结果
     */
    public int deleteScmCustomerOrdersById(Long id);

    /**
     * 批量删除客户订单
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteScmCustomerOrdersByIds(Long[] ids);

    /**
     * 批量删除客户订单明细
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteScmCustomerOrderItemsByOrderIds(Long[] ids);
    
    /**
     * 批量新增客户订单明细
     * 
     * @param scmCustomerOrderItemsList 客户订单明细列表
     * @return 结果
     */
    public int batchScmCustomerOrderItems(List<ScmCustomerOrderItems> scmCustomerOrderItemsList);
    

    /**
     * 通过客户订单主键删除客户订单明细信息
     * 
     * @param id 客户订单ID
     * @return 结果
     */
    public int deleteScmCustomerOrderItemsByOrderId(Long id);

    public ScmCustomerOrders selectScmCustomerOrdersByOrderCode(String orderCode);

    @Select("SELECT COUNT(*) FROM scm_customer_orders WHERE YEAR(create_time) = YEAR(#{yesterday}) AND MONTH(create_time) = MONTH(#{yesterday})")
    @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
    public int selectCustomerOrderCountByMonth(Date yesterday);

    @Select("SELECT COUNT(*) FROM scm_customer_orders WHERE YEAR(create_time) = YEAR(#{yesterday})")
    @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
    public int selectCustomerOrderCountByYear(Date yesterday);

    @Select("SELECT COUNT(*) FROM scm_customer_orders")
    @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
    public int selectCustomerOrderCountAll();

    @Select("SELECT COUNT(*) FROM scm_customer_orders WHERE payment_status <> 3 ")
    @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
    public int selectCustomerOrderCountNotPayed();

//    @Select("SELECT COUNT(*) FROM scm_customer_orders WHERE DATE_ADD(create_time, INTERVAL 5 DAY) < #{fiveDaysAgo}")
    @Select("SELECT COUNT(*) FROM scm_customer_orders WHERE requested_delivery_date < #{fiveDaysAgo}")
    @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
    public int selectCustomerOrderCountExpiring(Date fiveDaysAgo);
}
